package controllers;

import java.util.Calendar;
import java.util.List;

import play.mvc.With;
import client.RestException;
import client.YouGoClient;

import com.ineatconseil.yougo.dto.DetailedUserDto;
import com.ineatconseil.yougo.dto.RequestDto;
import com.ineatconseil.yougo.dto.RequestStatusDto;
import com.ineatconseil.yougo.dto.RequestTypeDto;

@With(Secure.class)
public class ValidRequests extends AbstractConroller {

	public static YouGoClient client = new YouGoClient();
	public static final String ACCEPTED_STATUS = "A";
	public static final String REFUSED_STATUS = "R";
	
	public static void index() throws RestException {
		List<DetailedUserDto> detailedUsers = getDetailedUsers();
		Calendar date = getCurrentDate();
		List<RequestTypeDto> types = getRequestTypes();
		int days = getDays();
		boolean now = true;
		render(detailedUsers, types, date, days, now);
	}

	//valid or refuse request
	public static void updateRequest(Long userId, Long requestId, String status, String comment) throws RestException {
		RequestDto request = new RequestDto();
		request = client.getRequestDtos(userId,requestId);
		//affect status for request
		if(status.equals(ACCEPTED_STATUS))
		{
			request.setStatus(RequestStatusDto.ACCEPTED);
		}
		else 
		{
			request.setStatus(RequestStatusDto.REFUSED);
		}
		request.setAnswerComment(comment);
		client.putRequestDtos(userId, request);
		cleanDetailledUser();
		}
	
	public static void today() throws RestException {
		List<DetailedUserDto> detailedUsers = getDetailedUsers();
		Calendar date = getCurrentDate();
		List<RequestTypeDto> types = getRequestTypes();
		int days = getDays();
		boolean now = true;
		render("ValidRequests/index.html", detailedUsers, types, date, days,
				now);
	}

	public static void paginate(Long time, int n) throws RestException {
		Calendar date = Calendar.getInstance();
		date.clear();
		date.setTimeInMillis(time);
		date.add(Calendar.DAY_OF_YEAR, n);
		boolean now = date.equals(getCurrentDate()) ? true : false;
		List<RequestTypeDto> types = getRequestTypes();
		List<DetailedUserDto> detailedUsers = getDetailedUsers();
		int days = getDays();

		render("ValidRequests/index.html", detailedUsers, types, date, days,
				now);
	}

	private static int getDays() {
		return 20;
	}

	private static Calendar getCurrentDate() {
		Calendar date = Calendar.getInstance();
		date.set(Calendar.HOUR, 0);
		date.set(Calendar.HOUR_OF_DAY, 0);
		date.set(Calendar.MINUTE, 0);
		date.set(Calendar.SECOND, 0);
		date.set(Calendar.MILLISECOND, 0);
		return date;
	}

}
